
(function($){
      var initMap = function(obj, opt){
    	var oldHtml = $(obj).html();
		var html = '<div style="width:100%;height:100%;">#{info}<div class="mod-map-tools">'+
				'<span class="mod-map-tools-btn mod-map-qp" data="qp">全屏 </span>'+
				'</div>'+
				'<div class="mod-down-tools">'+
					'<div class="mod-group">'+
						'<span class="mod-group-zoom"><i class="zoom-wx-off"></i><span>'+
					'</div>'+
					'<div class="mod-group">'+
						'<span class="mod-group-zoom"><i class="zoom-in"></i></span>'+
						'<span class="mod-group-zoom"><i class="zoom-out"></i></span>'+
					'</div>'+
				'</div>'+
				'<div id="#{id}View" class="mod-map-view"></div></div>';
		var id = $(obj).attr("id");
		html = html.replace("#{id}",id).replace("#{id}",id).replace("#{info}",oldHtml);	
		var qpobj = $(html);
		qpobj.find(".mod-map-qp").click(function(){
			if($(this).attr("data")=='qp'){
				$(this).attr("data", "tx");
				$(this).html("退出");
				qpobj.css("position","fixed");
				qpobj.css("z-index","100");
				qpobj.css("top","0px");
				qpobj.css("bottom","0px");
				qpobj.css("left","0px");
				qpobj.css("right","0px");
			} else {
				$(this).attr("data", "qp");
				$(this).html("全屏");
				qpobj.css("position","relative");
				qpobj.css("z-index","auto");
				$(obj).append(qpobj);
				opt.fullClose();
			}
		});
		qpobj.find(".mod-map-qp").click();
		if(opt.height == null){
			opt.height = $(window).height();
		}
		$(obj).css("height", opt.height - $(obj).offset().top-10)
		$(obj).html("");
		$(obj).append(qpobj);
		var map = new AMap.Map(id+"View", {
	        resizeEnable: true,
	        zoom: 15
	    });
		var satellLayer = null;
		qpobj.find(".zoom-wx-off").click(function(){
			if($(this).hasClass("zoom-wx-off")){
				$(this).removeClass("zoom-wx-off");
				$(this).addClass("zoom-wx-on");
				if(satellLayer == null){
					satellLayer = new AMap.TileLayer.Satellite({zIndex:-1}); //实例化卫星图
					satellLayer.setMap(map); //在map中添加卫星图
				}
				satellLayer.setzIndex(10); //在map中添加卫星图
			} else {
				$(this).addClass("zoom-wx-off");
				$(this).removeClass("zoom-wx-on");
				satellLayer.setzIndex(-1); //在map中添加卫星图
			}
		});
		qpobj.find(".zoom-in").click(function(){
			map.setZoom(map.getZoom()+1);
		});
		qpobj.find(".zoom-out").click(function(){
			map.setZoom(map.getZoom()-1);
		});
		qpobj.find(".mod-input").each(function(){
			$(this).bind('input propertychange',function(){
				if($(this).val().length > 14) {
					$(this).val($(this).val().substring(0,14));
				}
			});
		});
		map.on('click', function(e) {
	    	if(infoWindow != null) {
	    		infoWindow.close();
	    	}
	    });
		return map;
      }
      var options = {
    	 fullClose:function(){}
      };

      $.fn.extend({
          initMap:function(opt){
        	var opt2 = $.extend({}, options, opt||{});
          	return initMap(this, opt2);
          }
      });
})(jQuery);



(function($){
    var selectMap = function(opt){
    	var time = new Date().getTime();
    	var htmlObj = $('<div class="mod-map" id="container'+time+'">'+
			'<div class="mod-map-btns" style="">'+
				'<span id="mapSave'+time+'">保存</span>'+
			'</div>'+
		'</div>');
    	$("body").append(htmlObj);
    	var marker;
  		var lng, lat;
    	map = $("#container"+time).initMap({fullClose:function(){
    		htmlObj.remove();
    	}});
		
		if(opt.lng != null){
			var mark = new AMap.Marker({
	        	icon:"/img/qd.png",
	            map: map,
	            position: [opt.lng, opt.lat],
	            offset:new AMap.Pixel(-10, -24)
	        });
			map.setFitView();
			lng =opt.lng;
	  		lat =opt.lat;
		}
		
		
		map.on('mousemove', function(e){
			marker = mapMarkerAlert(marker, e.lnglat, "右键选择坐标");
			marker.show();
	    });
	  
	  	map.on('rightclick', function(e) {
	  		map.clearMap();
	  		var mark = new AMap.Marker({
	        	icon:"/img/qd.png",
	            map: map,
	            position: [e.lnglat.getLng(), e.lnglat.getLat()],
	            offset:new AMap.Pixel(-10, -24)
	        });
	  		lng =e.lnglat.getLng();
	  		lat =e.lnglat.getLat();
	  		
	    });
	  	
	  	
	  	$("#mapSave"+time).click(function(){
	  		if(lng == null){
	    		alert("还未选择坐标");
	    		return;
	    	}
	  		htmlObj.remove();
	  		opt.select(lng, lat);
	  	});
    }
    var options = {
    	select:function(){}
    };
    $.extend({
    	selectMap:function(opt){
        	var opt2 = $.extend({}, options, opt||{});
        	return selectMap(opt2);
        }
    });
})(jQuery);

var colors = ["#ff2be3","#5214e0","#007ab8", "#981010","#2e8e09", "#b5388f", "#8f00fc", "#ff398f", "#0e7f5b", "#1c3c9d", "#697b17"];
var colorIndex = 0;
function mapPolyline(map, hist) {
	var color = colors[colorIndex%12];
	var line = new Array();
	$.each(hist,function(i2,v2){
		line[i2] = [v2.longitude,v2.latitude];
	});
	_line=(function(){
        return new AMap.Polyline({
            map: map,
            path: line,
            showDir:true,
            zIndex:50,
            strokeColor: color,//线颜色
            strokeOpacity: 0.9,//线透明度
            strokeWeight: 2,//线宽
            strokeStyle: "solid"//线样式
        });
    })();
	var lineb = line[0];
	var linee = line[line.length-1];
	
	var marb = mapMarker(map, {position:lineb,color:color, type:"begin"});
	var mare = mapMarker(map, {position:linee,color:color, type:"end"});
	marb.on('click', function(e){
		infoWindow = mapWindow({list:[hist[0]]});
    	infoWindow.open(map,new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()));
	});
	mare.on('click', function(e){
		infoWindow = mapWindow({list:[hist[hist.length-1]]});
    	infoWindow.open(map,new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()));
	});
    _line.on('mouseover', function(e) {
    	_line.setOptions({
    		zIndex:51,
    		strokeOpacity: 1,//线透明度
            strokeWeight: 3,//线宽
        });
    	var distance = null;
    	var disV = null;
    	$.each(hist, function(i,v){
    		if(distance == null || distance > e.lnglat.distance([v.longitude,v.latitude])){
    			distance = e.lnglat.distance([v.longitude,v.latitude]);
    			disV = v;
    		}
    	});
    	disV.createdAtStr = new Date(disV.createdAt).Format("yyyy-MM-dd HH:mm");
    	var infoWindow = mapWindow({list:[disV]});
    	infoWindow.open(map,new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()));
    	
    });
    _line.on('mouseout', function(e) {
    	_line.setOptions({
    		strokeOpacity: 0.9,//线透明度
            strokeWeight: 2,//线宽
        });
    });
    colorIndex++;
    map.setFitView();
}


function mapMarker(map, opt2) {
	var typeColor = {GPS:"#ff0000",BDS:"#b63aaf",LBS:"#55afdb",WIF:"#63ba40",ACT:"#d4a63a",offline:"#999"}
	var label = '';
	if(opt2.type!=null){
		if(typeColor[opt2.type] == null || typeColor[opt2.type] == undefined){
			label = '<i class="map-marker-icon map-marker-icon-'+opt2.type+'"></i>';
		} else {
			opt2.color=typeColor[opt2.type];
		}
	}
	
	var options = {color:colors[0],label:label};
	var opt = $.extend({}, options, opt2||{});
	var marker;
	var content = '<div class="map-marker" style="background-color: '+opt.color+';">'+opt.label+'</div>';
	if(opt.marker != null){
		marker = opt.marker;
		marker.content=content;
	} else {
		marker = new AMap.Marker({
	        map: map,
	        content:content,
	        position: opt.position,
	        offset:new AMap.Pixel(-9, -9)
	    });
	}
	AMap.event.addListener(marker, 'mouseover', function() {
		marker.setzIndex(101);
    });
	AMap.event.addListener(marker, 'mouseout', function() {
		marker.setzIndex(100);
    });
	AMap.event.addListener(marker, 'click', function() {
		
		
//		var mar = new AMap.Marker({
//	        map: map,
//	        content:content,
//	        position: new AMap.Pixel(opt.position[0],opt.position[1]),
//	        draggable:true,
//	        raiseOnDrag:true,
//	        offset:new AMap.Pixel(-9, -9)
//	    });
    });
	
    return marker;
}

function mapPolygon(map,arr){
	_polygon=(function(){
		var polygon = new AMap.Polygon({
            map: map,
            path: arr,
            strokeColor: "#F33",
            strokeOpacity: 0.5,
            strokeWeight: 1,
            fillColor: "#ee2200",
            fillOpacity: 0.2
        });
		polygon.setMap(map);
        return polygon;
    })();
	return _polygon;
}
function mapCircle(lnglat, radius){
	_circle=(function(){
        var circle = new AMap.Circle({
            center: lnglat,// 圆心位置
            radius: radius, //半径
            strokeColor: "#F33", //线颜色
            strokeOpacity: 0.5, //线透明度
            strokeWeight: 3, //线粗细度
            fillColor: "#ee2200", //填充颜色
            fillOpacity: 0.2//填充透明度
        });
        circle.setMap(map);
        return circle;
    })();
	return _circle;
}

var infoWindow;
function mapWindow(data){
	var htmlTemp = '<div class="mod-wins">'+
					'<div style="overflow-y: auto;overflow-x: hidden;max-height:200px;">'+
					'#for(data:list)#'+
					'<div class="mod-win">'+
						'<div class="mod-th">#{data.fullName}#</div>'+
						'<div class="mod-line">'+
							' #{data.createdAtStrs}#'+
							' #{data.capacityStr}#'+
						'</div>'+
						" #{data.departmentNameStr}# "+
						" #{data.groupNameStr}# "+
						" #{data.phoneStr}# "+
						" #{data.dutiesStr}# "+
						" #{data.levelDescStr}# "+
						" #{data.travelTypeDescStr}# "+
						" #{data.distanceStr}# "+
						" #{data.arriveTimeStr}# "+
						' #{data.snStr}# '+
					'</div>'+
					'#end#'+
				'</div></div>';
	if(data ==null || data.list==null|| data.list.length==0){
		return null;
	}
	$.each(data.list, function(i,v){
		v.createdAtStrs = ((v.createdAtStr==null || v.createdAtStr=='')?"":'<span class="mod-l">#[comm.time]#：'+v.createdAtStr+'</span>');
		v.capacityStr = ((v.capacity==null || v.capacity=='')?"":'<span class="mod-r">#[comm.capacity]#：'+v.capacity+' %</span>');
		v.phoneStr = ((v.phone==null || v.phone=='')?"":'<div class="mod-line">#[comm.phone]#： '+v.phone+'</div>');
		v.levelDescStr = ((v.levelDesc==null || v.levelDesc=='')?"":'<div class="mod-line">#[sn.level]#： '+v.levelDesc+'</div>');
		v.dutiesStr = ((v.duties==null || v.duties=='')?"":'<div class="mod-line">#[comm.duties]#： '+v.duties+'</div>');
		v.travelTypeDescStr = ((v.travelTypeDesc==null || v.travelTypeDesc=='')?"":'<div class="mod-line">#[gps.conf.dbgj]#： '+v.travelTypeDesc+'</div>');
		v.distanceStr = ((v.distance==null || v.distance=='')?"":'<div class="mod-line">#[now.jl]#： '+v.distance+' #[mi]#</div>');
		v.arriveTimeStr = ((v.arriveTime==null || v.arriveTime=='')?"":'<div class="mod-line">#[arrive.time]#： '+v.arriveTime+' #[fen]#</div>');
		v.departmentNameStr = ((v.departmentName==null || v.departmentName=='')?"":'<div class="mod-line">#[comm.dep]#： '+v.departmentName+'</div>');
		v.groupNameStr = ((v.groupName==null || v.groupName=='')?"":'<div class="mod-line">#[group	]#： '+v.groupName+'</div>');
		v.snStr = ((v.sn==null || v.sn=='')?"":'<div class="mod-line">#[dep.sn]#： '+v.sn+'</div>');
	});
	var html = $.loadData({html:htmlTemp,data:data});
	
    //实例化信息窗体
    infoWindow = new AMap.InfoWindow({
        content: html,
        offset: new AMap.Pixel(0, -10)
    });
    return infoWindow;
}

function mapSignWindow(data) {
	var info = '<div class="info-win">'+
		'<div class="info"><span class="info-text">#{name}#</span></div>'+
		'<div class="info"><span class="info-text">#{signAtStr}#</span></div>'+
		'<div class="info">'+
			'<span class="info-text">#[wc]#</span>'+
			'<span class="info-text">#{deviationAt}# #[fen]#</span>'+
			'<span class="info-text">#{deviationDistance}# #[mi]#</span>'+
		'</div>'+
		'<div class="info-btn-group"><a class="info-btn" href="javascript:mapSave(#{id}#, #{longitude}#, #{latitude}#);">#[comm.upd]#</a><a class="info-btn" href="javascript:delData(#{id}#);">#[comm.del]#</a></div>'+
		'<div class="info-down"><i class="down"></i></div>'+
	'</div>';
	
	info = $.loadData({html:info,data:data});
	
	//实例化信息窗体
	if(infoWindow == null){
		infoWindow = new AMap.InfoWindow({
	        isCustom: true,  //使用自定义窗体
	        content: info,
	        offset:new AMap.Pixel(0, -35),
	    });
	} else {
		infoWindow.setContent(info);
	}
    
    return infoWindow;
}

var mapMarkerAlert=function(marker, lnglat, msg){
	var content = '<div class="marker-alert">'+msg+'</div>';
	if(marker != null){
		marker.setPosition(lnglat);
		marker.setContent(content);
	} else {
		marker = new AMap.Marker({
			content:content,
            map: map,
            position: lnglat,
            offset: new AMap.Pixel(6, 15)
        });
	}
	return marker;
}
var mapMarkerLabel = function(name){
	var info = '<div class="info-win" style="text-align: center;position: relative;padding: 3px;border: 1px solid silver;display: inline-block;background-color:#fff;border-radius: 3px;width:110px;">'+
		'<div style=""><span>'+name+'</span></div>'+
		'<div class="info-down"><i class="down" style="margin-top:3px;"></i></div>'+
	'</div>';
	return info;
}

